/**
 * useContext
 */
import React, { useState, useEffect } from 'react';
import { history, formatMessage, useIntl, getDvaApp } from 'umi';
import { Button } from 'antd';

export default props => {
  const [count, setCount] = useState(0);

  useEffect(() => {}, []);

  return (
    <div>
      <div>count:{count}</div>
      <Button
        onClick={() => {
          setCount(count + 1);
        }}
      >
        add
      </Button>
    </div>
  );
};

// 创建一个 context
const Context = createContext(0);

// 组件一, useContext 写法
function Comp() {
  const count = useContext(Context);
  return <div>{count}</div>;
}

function App() {
  const [count, setCount] = useState(0);
  return (
    <div>
      点击次数: {count}
      <button
        onClick={() => {
          setCount(count + 1);
        }}
      >
        点我
      </button>
      <Context.Provider value={count}>
        <Comp></Comp>
      </Context.Provider>
    </div>
  );
}
